package cn.com.cdb.ldms.components.threadpools;//package com.dcits.components.threadpools;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
//import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
//import org.springframework.context.ApplicationContext;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.stereotype.Component;
//
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.Executor;
//import java.util.concurrent.ThreadPoolExecutor;
//
//@Component
//@Endpoint(id = "threadpools")
//@Slf4j
//public class ThreadPoolEndpoint {
//    private final ExecutorRegistry registry;
//    private final ApplicationContext applicationContext;
//
//    ThreadPoolEndpoint(ApplicationContext applicationContext, ExecutorRegistry registry) {
//        this.registry = registry;
//        this.applicationContext = applicationContext;
//    }
//
//
//    @ReadOperation
//    public Map<String, Object> threadPools() {
//        Map<String, Object> result = new HashMap<>();
//
//        // 1. 收集自定义线程池
//        Map<String, Executor> customPools = registry.getExecutorMap();
//        customPools.forEach((name, executor) -> {
//            if (executor instanceof ThreadPoolTaskExecutor) {
//                ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) executor;
//                result.put(name, getThreadPoolInfo(taskExecutor.getThreadPoolExecutor()));
//            } else if (executor instanceof ThreadPoolExecutor) {
//                result.put(name, getThreadPoolInfo((ThreadPoolExecutor) executor));
//            }
//        });
//
//        // 2. 收集Spring Boot默认线程池（如异步任务线程池）
//        Map<String, ThreadPoolTaskExecutor> springPools = applicationContext.getBeansOfType(ThreadPoolTaskExecutor.class);
//        springPools.forEach((name, executor) -> {
//            if (!customPools.containsKey(name)) { // 避免重复
//                result.put(name, getThreadPoolInfo(executor.getThreadPoolExecutor()));
//            }
//        });
//
//        return result;
//    }
//
//    private Map<String, Object> getThreadPoolInfo(ThreadPoolExecutor executor) {
//        Map<String, Object> info = new HashMap<>();
//        info.put("corePoolSize", executor.getCorePoolSize());
//        info.put("maxPoolSize", executor.getMaximumPoolSize());
//        info.put("activeCount", executor.getActiveCount());
//        info.put("poolSize", executor.getPoolSize());
//        info.put("queueSize", executor.getQueue().size());
//        info.put("completedTaskCount", executor.getCompletedTaskCount());
//        info.put("taskCount", executor.getTaskCount());
//        info.put("largestPoolSize", executor.getLargestPoolSize());
//        return info;
//    }
//}